package com.spukmk3me.video;

/* $if SPUKMK3ME_DEBUG$ */
import com.spukmk3me.debug.Logger;
/* $endif$ */
import com.spukmk3me.scene.Entity;
import com.spukmk3me.video.Image;

public abstract class ImageRegion extends Entity
{
    protected ImageRegion( Image img, int x, int y, int w, int h )
    {
        super( img );
        
        if ( img == null )
        {
            img = null;
            x = y = w = h = 0;
        }
        else
        {
            /* $if SPUKMK3ME_DEBUG$ */
            if ( (x < 0) || (y < 0) || (x + w > img.width()) || (y + h > img.height()) )
                Logger.trace( "Image region is overlapped outside image." );
            /* $endif$ */
            
            x = Math.max( 0, Math.min( x, img.width() - 1 ) );
            y = Math.max( 0, Math.min( y, img.height() - 1 ) );
            w = Math.min( w, img.width() - x );
            h = Math.min( h, img.height() - y );
        }
        
        this.img = img;
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }
    
    public final byte getType()
    {
        return ET_IMAGE;
    }
    
    public final Image  img;
    public final int    x, y, w, h;
}
